<template>
  <!-- 人员类型配置列表 -->
  <div class="app-container">
    <div class="form-container">
      <el-form
        :inline="true"
        label-width="80px"
        :model="filters"
        class="selectFrom"
      >
        <el-form-item label="类型名称" prop="typeName">
          <el-input
            v-model="filters.typeName"
            size="small"
            placeholder="请输入类型名称"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <el-button v-clickForbidden size="middle" icon="el-icon-search" @click="handleSearch()">搜索 </el-button>
      <el-button v-clickForbidden size="middle" icon="el-icon-refresh" @click="handleReset()">重置 </el-button>
      <el-button v-hasPermi="['supervise:certificateTemplate:list']" size="middle" icon="el-icon-plus" :disabled="single" @click="addCertTemplate()">配置注册信息</el-button>
      <el-button v-hasPermi="['supervise:configEnterpType:add']" icon="el-icon-setting" size="middle" :disabled="single" @click="handleConfigType">配置单位类型</el-button>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="tableLoading"
      class="table-list"
      stripe
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :data="tableData"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column prop="typeName" label="类型名称" min-width="180" align="center" />
      <el-table-column prop="typeNum" label="类型编号" min-width="40" align="center" />
      <el-table-column prop="orderNum" label="排序" min-width="40" align="center" />
      <!-- <el-table-column prop="enterpTypeNum" label="已配单位类型" min-width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.enterpTypeNum }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="remark"
        :show-overflow-tooltip="true"
        label="备注"
        min-width="180"
        align="center"
      />
    </el-table>
    <!-- 表格展示end-->
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 配置单位类型-->
    <ConfigType ref="ConfigType" />
  </div>
</template>

<script>
import { listPersonSupperType } from '@/api/superviseBaseInfo/typeManage/personnelType'
import { mapState } from 'vuex'
// 配置单位类型
import ConfigType from './components/ConfigType'
export default {
  name: 'PersonneType',
  components: { ConfigType },
  data() {
    return {
      tableLoading: false, // 表格加载
      highLevelSet: false, // 高级搜索
      // 筛选条件
      filters: {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      },
      /* 列表 start */
      // 分页总条数
      total: 0,
      // 非单个禁用
      single: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 表格数据
      tableData: []
      /* 列表 end */
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    // 获取数据
    this.getList()
  },
  methods: {
    // 新增证件模板
    addCertTemplate() {
      const id = this.multipleSelection[0].id
      this.$router.push({
        path: '/systemConfig/certTypeTemplate',
        query: { id }
      })
    },
    // 获取数据
    async getList() {
      this.tableLoading = true
      try {
        const { data: { rows, total }} = await listPersonSupperType(this.filters)
        this.tableData = rows
        this.total = total
        this.tableLoading = false
        // this.addTaleData()
      } catch {
        this.tableLoading = false
      }
    },
    // 补空白行
    addTaleData() {
      if (this.tableData.length < this.filters.pageSize) {
        var arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表单选中行事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else { // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    /* 表单选择行事件 end */

    /* 按钮 start */
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置搜索条件
    handleReset() {
      this.filters = {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      }
      this.getList()
    },
    // 配置单位类型
    handleConfigType() {
      const { typeNum, id } = this.multipleSelection[0]
      this.$refs.ConfigType.handleAdd('配置单位类型', { typeNum, kind: '0' }, id)
    }
    /* 按钮 end */
  }
}
</script>

